using System;
using System.IO;
using System.Windows.Forms;

namespace SpellCenter.UI.FileChoosing
{
    public partial class SavePage : UserControl, BackNextCancelControl.IPageValidator
    {

        public SavePage()
        {
            InitializeComponent();
            RegisterEvents();
        }

        public int PageId = 0;

        #region Events

        private void RegisterEvents()
        {
            _writingSystemBox.TextChanged += WritingSystemBox_TextChanged;
            _dictFileBox.TextChanged += WritingSystemBox_TextChanged;
            _dictFileSelectButton.Click += DictSelectButton_Click;
            VisibleChanged += Control_VisibleChanged;
        }

        private void Control_VisibleChanged(object sender, EventArgs e)
        {
            if(Visible)
            {
                WritingSystemBox_TextChanged(this, EventArgs.Empty);
            }
        }

        private void WritingSystemBox_TextChanged(object sender, EventArgs e)
        {
            if(IsWritingSystemValid && IsDictionaryValid)
            {
                OnValid(new BackNextCancelControl.PageNextEventArgs(PageId));
            }
            else
            {
                OnInvalid(new BackNextCancelControl.PageNextEventArgs(PageId));
            }
        }

        private void DictSelectButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Dictionary Files (*.dic)|*.dic|All Files (*.*)|*.*";
            if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                _dictFileBox.Text = dialog.FileName;
            }
        }

        #endregion


        public string WritingSystemID
        {
            get { return _writingSystemBox.Text; }
            set
            {
                _writingSystemBox.Text = value;
            }
        }

        private bool IsWritingSystemValid
        {
            get { return !string.IsNullOrEmpty(WritingSystemID); }
        }

        private bool IsDictionaryValid
        {
            get { return DictionaryFile == string.Empty || File.Exists(DictionaryFile); }
        }

        public string DictionaryFile
        {
            get { return _dictFileBox.Text; }
        }

        #region IPageValidator Members

        public bool IsValid()
        {
            return IsWritingSystemValid && IsDictionaryValid;
        }

        public event BackNextCancelControl.PageNextEventHandler Valid;

        protected void OnValid(BackNextCancelControl.PageNextEventArgs e)
        {
            BackNextCancelControl.PageNextEventHandler handler = Valid;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        
        public event BackNextCancelControl.PageNextEventHandler Invalid;

        protected void OnInvalid(BackNextCancelControl.PageNextEventArgs e)
        {
            BackNextCancelControl.PageNextEventHandler handler = Invalid;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion
    }
}
